package com.speciality.demo.demos.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.speciality.demo.demos.dto.DisasterStatDTO;
import com.speciality.demo.demos.entity.WeatherHourly;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface WeatherHourlyMapper extends BaseMapper<WeatherHourly> {
    List<WeatherHourly> selectClimateTrendByTimeRange(
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime
    );

    /**
     * 按时间范围查询灾害数据（热力图用）
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 区域灾害统计数据
     */
    List<DisasterStatDTO> selectDisasterByTimeRange(
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime
    );
    /**
     * 获取指定日期范围内的小时数据
     */
    @Select("SELECT * FROM weather_hourly WHERE DATE(time) BETWEEN #{startDate} AND #{endDate} ORDER BY time")
    List<WeatherHourly> selectByDateRange(@Param("startDate") LocalDate startDate,
                                          @Param("endDate") LocalDate endDate);

    /**
     * 获取小时级温度统计
     */
    @Select("SELECT AVG(temperature2m) as avg_temp, MAX(temperature2m) as max_temp, MIN(temperature2m) as min_temp FROM weather_hourly WHERE DATE(time) BETWEEN #{startDate} AND #{endDate}")
    Map<String, Object> selectHourlyTemperatureStats(@Param("startDate") LocalDate startDate,
                                                     @Param("endDate") LocalDate endDate);

    /**
     * 获取异常检测相关指标
     */
    @Select("SELECT AVG(temperature2m) as temp, AVG(relativehumidity2m) as humidity, AVG(windspeed10m) as wind_speed, AVG(pressure_msl) as pressure FROM weather_hourly WHERE DATE(time) = #{date}")
    Map<String, Object> selectDailyMetrics(@Param("date") LocalDate date);


    List<WeatherHourly> selectByDate(@Param("date") LocalDate date);
}
